@extends('layouts.layouts')
@section('title')
    <title>后台首页</title>
@endsection
@section('content')
    <link rel="stylesheet" href="/lib/layer/theme/default/layer.css"/>
    <script type="text/javascript" src="/lib/layer/layer.js"></script>
    <div class="content">
        <div class="header">
            <ul class="breadcrumb">
                <li><a href="/article/list">文章管理</a></li>
                <li class="active">文章列表</li>
            </ul>
        </div>
        <div class="main-content">
            <div class="btn-toolbar list-toolbar">
                <form class="form-inline col-sm-6" action="" method="get">
                    <a href="/article/add"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" ><i class="fa fa-plus"></i> 新增</button></a>
                    <div class="form-group">
                        <input type="text" class="form-control" name="title" placeholder="课程标题" value="">
                    </div>
                    <div class="form-group">
                        <select class="form-control" name="state">
                          <option value="2">全部</option>
                          <option value="1">已入库</option>
                          <option value="0">未入库</option>
                        </select>
                      </div>
                    <button type="submit" class="btn btn-default">筛选</button>
                </form>
            </div>

            <div class="row">
                <div class="col-sm-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading no-collapse">课程列表</div>
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th width="5%">编号</th>
                                    <th width="10%">文章名称</th>
                                    <th width="10%">文章链接</th>
                                    <th width="10%">标签信息</th>
                                    <th width="5%">用户名</th>
                                    <th width="10%">添加时间</th>
                                    <th width="5%">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($list as $v)
                                <tr id="articleTr_{{$v->id}}">
                                    <td>{{$v->id}}</td>
                                    <td>{{$v->title}}</td>
                                    <td>{{$v->url}}</td>
                                    <td>{{$v->tag_ids}}</td>
                                    <td>{{$v->author->name}}</td>
                                    <td>{{$v->created_at}}</td>
                                    <td>
                                        @if($v->state==0)
                                        <a class="btn btn-success btn-xs" href="/recommend/state?id={{$v->id}}&state=1">确认入库</a>
                                        @else
                                        <a class="btn btn-danger btn-xs" href="javascript:;">已入库</a>
                                        @endif
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            {{$list->appends(['title'=>$title])->render()}}
        </div>
    </div>
    <script type="text/javascript">
        //修改文章状态
        function articleState(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-state');
            if(state == 1){
                var notice = '确定下架吗？';
                var newState = 0;
                var name = '上架'
            }else{
                var notice = '确定上架吗？';
                var newState = 1;
                var name = '下架';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/article/changestate',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-state',newState);
                            $(object).text(name);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }

    </script>
@endsection